package com.chrissy.music.common.result;

import com.chrissy.music.common.enums.HttpCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * author Johnson
 * @author Johnson
 * @param <T>
 */
@Schema(description = "响应实体类")
@Data
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -637415113996231595L;

    @Schema(description = "响应时间")
    private LocalDateTime timestamp;

    @Schema(description = "响应状态值")
    private Integer code;

    @Schema(description = "响应消息")
    private String message;

    @Schema(description = "响应数据")
    private transient T data;


    public Result() {
    }

    public Result(Integer code, String message, T data) {
        this.timestamp = LocalDateTime.now();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success() {
        return new Result<>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> error() {
        return new Result<>(HttpCodeEnum.ERROR.getCode(), HttpCodeEnum.ERROR.getMessage(), null);
    }

    public static <T> Result<T> error(T data) {
        return new Result<>(HttpCodeEnum.ERROR.getCode(), HttpCodeEnum.ERROR.getMessage(), data);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(HttpCodeEnum.ERROR.getCode(), message, null);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message, null);
    }

    public Result(Integer status, String message) {
        this(status, message, null);
    }

}
